<template>
    <div class="message-wrap">
        <div
            class="page-title"
        >
            消息通知
        </div>
        <el-tabs
            v-model="activeName"
            @tab-click="fnChangeType"
        >
            <el-tab-pane
                label="全部消息"
                name="all"
            />
            <el-tab-pane
                label="订单消息通知"
                name="order"
            />
            <el-tab-pane
                label="订单维权通知"
                name="rights"
            />
            <el-tab-pane
                label="系统消息通知"
                name="system"
            />
        </el-tabs>
        <el-dropdown
            class="filter"
            size="mini"
            @command="fnFlter"
        >
            <el-button
                type="default"
                size="mini"
                class="filter-btn"
            >
                {{ filter }}<i class="el-icon-arrow-down zhedie" />
            </el-button>
            <el-dropdown-menu
                slot="dropdown"
                class="drop-menus"
            >
                <el-dropdown-item
                    command="全部"
                >
                    全部
                </el-dropdown-item>
                <el-dropdown-item
                    command="已读"
                >
                    已读
                </el-dropdown-item>
                <el-dropdown-item
                    command="未读"
                >
                    未读
                </el-dropdown-item>
            </el-dropdown-menu>
        </el-dropdown>
        <div class="table-wrapper">
            <el-table
                ref="multipleTable"
                :data="tableData"
                tooltip-effect="dark"
                height="100%"
                style="width: 100%;"
                :show-header="false"
                :span-method="arraySpanMethod"
                :header-cell-class-name="cellClassMethod"
                @selection-change="handleSelectionChange"
            >
                <el-table-column
                    type="selection"
                    width="55"
                />
                <el-table-column
                    label="内容"
                    props="content"
                    class="special"
                >
                    <div
                        slot-scope="scope"
                        class="content-wrap"
                        :class="{'has-read': scope.row.read_status == 1}"
                    >
                        {{ scope.row.message_content }}
                    </div>
                </el-table-column>
                <el-table-column
                    prop="create_time"
                    label="时间"
                    width="200"
                >
                    <div
                        slot-scope="scope"
                        style="color: #999;"
                    >
                        {{ scope.row.create_time*1000 | dateFormat('yyyy-MM-dd HH:mm:ss') }}
                    </div>
                </el-table-column>
                <el-table-column
                    label="操作"
                    props="content"
                    width="70"
                >
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            @click="fnShowMessageDialog(scope.row)"
                        >
                            详情
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <!-- 分页 -->
        <div class="pagination-wrapper clearfix">
            <div class="operates">
                <el-checkbox
                    v-model="allChecked"
                    :indeterminate="isIndeterminate"
                    @change="fnSelectAll"
                    class="all-checkbox"
                />
                <el-button
                    type="text"
                    @click="fnMarkReadBatch"
                >
                    标记已读
                </el-button>
                <el-button
                    type="text"
                    @click="fnDeleteBatch"
                >
                    删除
                </el-button>
            </div>
            <el-pagination
                background
                layout="total, prev, pager, next, jumper"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="fnGetData"
            />
        </div>
        <el-dialog
            :title="currentMessage.type"
            :visible.sync="showMessageDialog"
            width="640px"
            :before-close="fnCloseMessageDialog"
            custom-class="message-detail-dialog"
            :close-on-click-modal="false"
        >
            <div class="message-detail">
                {{ currentMessage.message_content }}
            </div>
            <span
                slot="footer"
                class="dialog-footer"
            >
                <el-button
                    type="primary"
                    size="medium"
                    @click="showMessageDialog = false"
                    class="message-dialog-btn"
                >知道了</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    components: {},
    data() {
        return {
            activeName: 'all',
            tableData: [],
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0,
                total_page: 0
            },
            loading: false,
            selection: [],
            allChecked: false,
            isIndeterminate: false,
            currentMessage: {},
            showMessageDialog: false,
            // tabs: [{
            //     name: 'all',
            //     title: '全部通知'
            // },{
            //     name: 'order',
            //     title: '订单消息通知'
            // },{
            //     name: 'system',
            //     title: '系统消息通知'
            // },{
            //     name: 'rights',
            //     title: '维权订单消息通知'
            // }],
            messageTypeList: {
                order: '订单消息',
                system: '系统消息',
                rights: '维权订单消息'
            },
            filter: '全部'
        };
    },
    watch: {
        $route(val){
            if(val.query.active && this.activeName !== val.query.active){
                this.activeName = val.query.active;
                this.fnGetData(1);
            }
        }
    },
    created(){
        if(this.$route.query.active && this.messageTypeList[this.$route.query.active]){
            this.activeName = this.$route.query.active;
        }
        this.$store.commit('header/SET_HEADER', [
            {title: '店铺概况', path: '/'},
            {title: '消息通知'}
        ]);
        this.fnGetData(1);
    },
    methods: {
        arraySpanMethod({ row, column, rowIndex, columnIndex }) {
            if (row.isBottom) {
                if (columnIndex === 1) {
                    return [1, 4];
                } else{
                    return [0, 0];
                }
            }
        },
        cellClassMethod({ row, column, rowIndex, columnIndex }){
            if (row.isBottom) {
                return 'bottom-tip';
            }
        },
        fnUpdateMessage(){
            this.$post('/Sidebar/getData', {}, resp => {
                if(resp.code == 1){
                    this.$store.commit('message/INIT_DATA', resp.data);
                }
            });
        },
        fnChangeType(item){
            this.fnGetData(1);
            this.$router.replace({
                query: {
                    active: this.activeName
                }
            });
        },
        fnFlter(command){
            this.filter = command;
            this.fnGetData(1);
        },
        fnGetData(page){
            let filterParam = '';

            if (this.filter == '已读') {
                filterParam = 1;
            } else if (this.filter == '未读') {
                filterParam = 0;
            } else {
                filterParam = '';
            }
            this.$post('/Message/getList', {
                read_status: filterParam,
                page_id: page || this.page.now_page,
                message_type: this.activeName == 'all' ? '' : this.activeName
            }, resp => {
                if (resp.code == 1) {

                    if(resp.data.page.total_page > 0 && resp.data.page.total_page < page){
                        this.fnGetData(resp.data.page.total_page);
                        return ;
                    }
                    if (resp.data.list.length == 0) {
                        this.init = true;
                    }else{
                        resp.data.list.push({
                            isBottom: true,
                            create_time: 0,
                            message_content: "只保留近12个月的消息",
                            message_id: 0,
                            message_type: "",
                            read_status: 0
                        });
                    }
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;

                }
            });
        },
        handleSelectionChange(selection){
            console.log('handleSelection');
            if(selection.length == this.tableData.length){
                this.allChecked = true;
                this.isIndeterminate = false;
            }else if(selection.length == 0){
                this.allChecked = false;
                this.isIndeterminate = false;
            }else{
                this.allChecked = false;
                this.isIndeterminate = true;
            }
            this.selection = selection;
        },
        fnGoDetail(item){},
        fnMarkRead(ids){
            this.$post('/Message/read', {
                message_ids: ids
            }, () => {
                this.fnUpdateMessage();
            });

        },
        fnMarkReadBatch(){
            if(this.selection.length <= 0){
                this.$notify.warning({
                    title: '',
                    duration: 2000,
                    message: '请至少选择一项'
                });
                return ;
            }
            let ids = this.selection.reduce((res, item) => {
                res.push(item.message_id);
                return res;
            }, []);

            this.$post('/Message/read', {
                message_ids: ids.toString()
            }, resp => {
                if(resp.code == 1){
                    this.$notify.success({
                        title: '',
                        duration: 2000,
                        message: '操作成功'
                    });
                    this.fnGetData();
                    this.fnUpdateMessage();
                }else{
                    this.$notify.error({
                        title: '错误',
                        duration: 2000,
                        message: resp.msg
                    });
                }
            });
        },
        fnDeleteBatch(){
            if(this.selection.length <= 0){
                this.$notify.warning({
                    title: '',
                    duration: 2000,
                    message: '请至少选择一项'
                });
                return ;
            }
            let ids = this.selection.reduce((res, item) => {
                res.push(item.message_id);
                return res;
            }, []);

            this.$post('/Message/clear', {
                message_ids: ids.toString()
            }, resp => {
                if(resp.code == 1){
                    this.$notify.success({
                        title: '',
                        duration: 2000,
                        message: '操作成功'
                    });
                    this.fnGetData();
                    this.fnUpdateMessage();
                }else{
                    this.$notify.error({
                        title: '错误',
                        duration: 2000,
                        message: resp.msg
                    });
                }
            });
        },
        fnSelectAll(value){
            this.$refs.multipleTable.toggleAllSelection();
        },
        fnShowMessageDialog(item){
            item.type = this.messageTypeList[item.message_type] ? this.messageTypeList[item.message_type] : '消息';
            this.currentMessage = item;
            this.showMessageDialog = true;
            if(item.read_status != 1){
                this.fnMarkRead(item.message_id);
                this.$set(item, 'read_status', 1);
            }
        },
        fnCloseMessageDialog(){
            this.currentMessage = {};
            this.showMessageDialog = false;
        },
    }
};

</script>
<style lang="less">
.message-wrap{
    position: relative;
    .el-tabs__header{
        margin-bottom:0;
    }
    .el-tabs__item{
        padding: 0 20px!important;
        box-sizing: border-box;
        &:hover{
            background: #F9F9F9!important;
        }
    }
    .el-tabs__nav-wrap::after{
        background-color: transparent;
    }
    .el-table{
        color: #333;
    }
    .el-table td, .el-table th{
        padding: 0;
        height: 56px;
    }
    .el-table__row:last-child{
        .content-wrap{
            font-size: 12px;
            font-weight: 400;
            color: #999999;
            line-height: 12px;
            padding: 24px 0;
            text-align: center;

        }
    }
}
.drop-menus{
    width: 80px!important;
    .el-dropdown-menu__item{
        text-align: center;
    }
}
.message-detail-dialog{
    .el-dialog__header{
        padding: 0 32px!important;
        height: 44px;
        line-height: 44px;
        background: #FAFAFA;
        box-shadow: 0px 1px 0px 0px #E9E9E9;
        border-radius: 4px 4px 0px 0px;
        font-size: 14px;
        font-weight: 500;
        color: #333333;
    }
    .el-dialog__headerbtn{
        top: 15px;
    }
    .message-detail{
        width: 532px;
        background: #FBFBFB;
        border-radius: 4px;
        border: 1px solid #EEEEEE;
        padding: 24px;
        box-sizing: border-box;
        margin-left: 30px;
        font-size: 14px;
        font-weight: 400;
        color: #333333;
        line-height: 22px;

    }
    .el-dialog__footer{
        display: flex;
        justify-content: center;
        align-items: center;
    }
}
</style>
<style lang="less" scoped>
.message-wrap{
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #fff;
    height: 100%;
    overflow: auto;
    padding: 24px;
}
.page-title{
    height: 22px;
    font-size: 16px;
    font-weight: 500;
    color: #333333;
    line-height: 22px;
    margin-bottom: 12px;

}
.table-wrapper {
    flex: 1;
    overflow: hidden;
    border-top: 1px solid #F0F2F5;
}
.pagination-wrapper{
    margin-top: 20px;
    .all-checkbox{
        margin-left: 14px;
        margin-right: 12px;
    }
    .operates{
        display: inline-block;
    }
    .el-pagination {
        float: right;
    }
}
.table-lines{
    border-top: 1px solid #F0F2F5;
}
.table-line{
    display: flex;
    align-items: center;
    flex-direction: row;
    border-bottom: 1px solid #F0F2F5;
}
.table-line-content{
    flex: 1;
    line-height: 40px;
    font-size: 14px;
    font-weight: 400;
    color: #333333;
    padding: 0 20px;

}
.table-line-time{
    padding: 0 20px;
    font-size: 14px;
    font-weight: 400;
    color: #999999;
    line-height: 40px;

}
.table-line-operate{
    padding: 0 20px;
}
.table-tip{
    text-align: center;
    font-size: 12px;
    font-weight: 400;
    color: #999999;
    line-height: 40px;
    margin-top: 10px;
}
.content-wrap{
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 14px;
}
.has-read{
    color: #999;
}
.message-dialog-btn{
    padding:0 40px;
    font-size: 14px;
    font-weight: 500;
    color: #FFFFFF;
    line-height: 36px;

}
.bottom-tip{
    color:orange;
}
.filter{
    position: absolute;
    right: 40px;
    top: 60px;
}
.filter-btn{
    width: 80px;
    height:32px;
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: center;
    color: #666;
    .el-icon-arrow-down{
        margin-left: 10px;
        transform: rotate(-90deg);
    }
}
</style>